package com.xczl.book.test.service;

import java.util.Date;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.shishuo.cms.entity.vo.PageVo;
import com.xczl.book.entity.Borrow;
import com.xczl.book.entity.vo.BorrowVo;
import com.xczl.book.service.BorrowService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class BorrowServiceTestCase {
	
	@Autowired
	private BorrowService borrowService;
	
	/**
	 * 添加借阅记录
	 */
	@Test
	@Transactional
	public void addBorrow() {
		Borrow borrow = createBorrow();
		borrowService.add(borrow);
		Borrow temp = borrowService.findById(borrow.getBorrowId());
		Assert.notNull(temp);
	}
	
	/**
	 * 创建借阅记录
	 * 
	 * @return
	 */
	private Borrow createBorrow() {
		Borrow borrow = new Borrow();
		Date now=new Date();
		borrow.setBookId(1);
		borrow.setUserId(3);
		borrow.setBorrowTime(now);
		borrow.setReturnTime(now);
		borrow.setState(0);
		return borrow;
	}
	
	/**
	 * 修改商品
	 */
	@Test
	@Transactional
	public void update() {
		// 注册
		Borrow borrow = createBorrow();
		borrowService.add(borrow);
		// 修改商品
		borrow.setState(1);
		borrowService.update(borrow.getBorrowId(),1);
		// 获取更新信息
		Borrow borrow1 = borrowService.findById(borrow.getBorrowId());
		// 验证结果
		Assert.isTrue(borrow.getState().equals(borrow1.getState()));
	}
	
	/**
	 * 查询
	 */
	@Test
	@Transactional
	public void find() {
		BorrowVo filter = new BorrowVo();
		filter.setName("X");
		PageVo<BorrowVo> page = borrowService.findByCondition(1, filter);
		for (BorrowVo borrow : page.getList()) {
			System.out.println(borrow.toString());
		}
	}
}
